package com.itheima.web;

import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 1.接收数据，封装Brand对象
        String brandName = request.getParameter("brandName");
        String companyName = request.getParameter("companyName");
        String orderedStr = request.getParameter("ordered");
        String description = request.getParameter("description");
        String statusStr = request.getParameter("status");

        // 字符串转成数字
        int ordered = Integer.parseInt(orderedStr);
        int status = Integer.parseInt(statusStr);

        Brand brand = new Brand(0, brandName, companyName, ordered, description, status);
        // 2.调用service完成添加
        BrandService brandService = new BrandService();
        brandService.add(brand);

        // 3.转发到查询所有Servlet
        request.getRequestDispatcher("/selectAllServlet").forward(request, response);
    }
}
